// asyncRouter.js
import {defaultRouter, addRouter} from '@/router/index'
//判断当前角色是否有访问权限
export function hasPermission(roles, route) {

  if (route.meta && route.meta.roles) {
    console.log(roles);
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}


export function GetAllRouter(){
  var all = []
  Object.assign(all,defaultRouter)
  Object.assign(all,addRouter)
}

// 递归过滤异步路由表，筛选角色权限路由
export function filterAsyncRoutes(routes, roles) {
  const res = [];
  if (routes === undefined) {
    return res
  }

  routes.forEach(route => {
    const tmp = { ...route }
    console.log(typeof(roles));
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}